package com.qbkj.base_lib.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.qbkj.base_lib.R;
import com.qbkj.base_lib.utils.ViewController;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public abstract class BaseActivity extends FrameBaseActivity {
    private static final String TAG = BaseActivity.class.getSimpleName();
    private View basestatus_view;
    private FrameLayout content_view, toproot_view;
    protected ViewController v;

    @Override
    protected void onQbkjCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_base);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        resetStatusViewHeight();
        initView();
        initTopView();
        init(savedInstanceState);
    }

    protected abstract int setContentLayoutId();

    protected abstract void init(Bundle savedInstanceState);

    //初始化布局
    private void initView() {
        content_view = findViewById(R.id.content_view);
        View view = LayoutInflater.from(this).inflate(setContentLayoutId(), null, false);
        ViewGroup.LayoutParams layoutParams = content_view.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        content_view.setLayoutParams(layoutParams);
        content_view.addView(view);
        v = new ViewController(this, getWindow());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.back_btn) {
            finish();

        }
    }

    //初始化默认头布局
    private void initTopView() {
        toproot_view = (FrameLayout) findViewById(R.id.toproot_view);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_toolbar, toproot_view, false);
        view.findViewById(R.id.back_btn).setOnClickListener(this);
        view.findViewById(R.id.menu_btn).setOnClickListener(this);
        toproot_view.addView(view);
        setTopLayoutParams(view);
    }


    //替换头布局
    protected void setTopView(int topViewlayoutId) {
        View topView = LayoutInflater.from(this).inflate(topViewlayoutId, toproot_view, false);
        for (int i = 0; i < toproot_view.getChildCount(); i++) {
            toproot_view.getChildAt(i).setVisibility(View.GONE);
        }
        toproot_view.addView(topView);
        setTopLayoutParams(topView);
    }

    private void setTopLayoutParams(final View topView) {
        toproot_view.post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = toproot_view.getLayoutParams();
                layoutParams.height = topView.getHeight();
                layoutParams.width = topView.getWidth();
                toproot_view.setLayoutParams(layoutParams);
            }
        });

    }


    //将状态栏高度赋给basestatus_view
    private void resetStatusViewHeight() {
        basestatus_view = findViewById(R.id.basestatus_view);
        int statusheight = getStatusBarHeight();
        if (statusheight != -1) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusheight);
            basestatus_view.setLayoutParams(params);
        }

    }

    //是否显示状态栏，默认显示
    protected void showStatusView(boolean show) {
        if (!show) basestatus_view.setVisibility(View.GONE);
        else basestatus_view.setVisibility(View.VISIBLE);
    }


    //隐藏头布局
    public void hideTopView() {
        toproot_view.setVisibility(View.GONE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
